package com.example.test.firstapp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.*;
import android.widget.*;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import android.os.Handler;
import android.os.Message;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    public static final int MSG_CODE = 1;
    //创建button变量；
    Button IniButtonVar;
    TextView IniStatusVar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
            //初始化button变量
        IniButtonVar=(Button)findViewById(R.id.IniButton);
        IniStatusVar=(TextView)findViewById(R.id.IniStatus) ;
        IniStatusVar.setMovementMethod(ScrollingMovementMethod.getInstance());
        //添加按钮响应函数
        IniButtonVar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(
                        new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Socket socket = null;
                                    socket = new Socket("219.223.193.99", 8080);
                                    //DataInputStream dataInput = new DataInputStream(socket.getInputStream());//从服务器收到的数据
                                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                                    out.writeUTF("12345");
                                    Log.d(this.toString(),"数据已发送到客户端");
                                    out.close();
                                    socket.close();
                                    IniStatusVar.setText("已发送");
                                }catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                ).run();

            }

        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            Bundle data = msg.getData();
            String val = data.getString("value");
            Log.i("mylog", "请求结果为-->" + val);
            // TODO
            // UI界面的更新等相关操作
            try {
                Socket socket = null;
                socket = new Socket("219.223.193.99", 8080);
                //DataInputStream dataInput = new DataInputStream(socket.getInputStream());//从服务器收到的数据
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                out.writeUTF("12345");
                Log.d(this.toString(),"数据已发送到客户端");
                out.close();
                socket.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    /**
     * 网络操作相关的子线程
     */
    Runnable mynetworktask = new Runnable() {

        @Override
        public void run() {
            // TODO
            // 在这里进行 http request.网络请求相关操作
            Message msg = new Message();
            Bundle data = new Bundle();
            data.putString("value", "请求结果");
            msg.setData(data);
            handler.handleMessage(msg);

        }
    };


}
